#include "portal_sql.h"
#include "portal_user.h"
#include "portal_nas.h"

static int portal_nas_table_read(void *arg, int argc, char *argv[], char **colname)
{
    struct in_addr acip;

    if (argc != 2) {
        PORTAL_DEBUG("Invalid argc %d.\n", argc);
        return 0;
    }

    if (!inet_aton(argv[0], &acip)) {
        PORTAL_DEBUG("invalid acip %s", argv[0]);
        return 0;
    }

    if (!portal_add_acip(strlen(argv[1]) > 0 ? argv[1] : NULL, acip.s_addr)) {
        PORTAL_DEBUG("add acip %s failed", argv[0]);
        return 0;
    }

    printf("Add acip %s, acname %s success.\n", argv[0], argv[1]);

    return 0;
}

static int portal_mac_table_read(void *arg, int argc, char *argv[], char **colname)
{
    uchar usermac[6];

    if (argc != 3) {
        PORTAL_DEBUG("Invalid argc %d.\n", argc);
        return 0;
    }

    if (strlen(argv[0]) != 12 || strlen(argv[1]) >= 253 || strlen(argv[2]) >= 65) {
        PORTAL_DEBUG("Invalid usermac %s, username %s password %s.\n", argv[0], argv[1], argv[2]);
        return 0;
    }

    portal_str_to_hex(argv[0], strlen(argv[0]), usermac);
    portal_add_bindmac_user(usermac, argv[1], argv[2]);
    printf("Add usermac %s, username %s password %s.\n", argv[0], argv[1], argv[2]);

    return 0;
}

bool portal_sql_init()
{
    int sql_ret;
    char sql_buf[1024];
    char *sql_errmsg;

    sql_ret = sqlite3_open(portal_args.portal_db, &portal_args.portal_table);
    if (sql_ret != SQLITE_OK) {
        printf("Init db failed: %s.\n", sqlite3_errmsg(portal_args.portal_table));
        return FALSE;
    }

    snprintf(sql_buf, sizeof(sql_buf), "create table if not exists nas_table (nasip varchar[16], nasname varchar[64])");
    sql_ret = sqlite3_exec(portal_args.portal_table, sql_buf, NULL, NULL, &sql_errmsg);
    if (sql_ret != SQLITE_OK) {
        printf("create nas table db failed: %s, sql_ret %d.\n", sqlite3_errmsg(portal_args.portal_table), sql_ret);
        sqlite3_free(sql_errmsg);
        sqlite3_close(portal_args.portal_table);
        return FALSE;
    }

    snprintf(sql_buf, sizeof(sql_buf), "create table if not exists mac_table "
             "(usermac varchar[12], username varchar[254], password varchar[64])");
    sql_ret = sqlite3_exec(portal_args.portal_table, sql_buf, NULL, NULL, &sql_errmsg);
    if (sql_ret != SQLITE_OK) {
        printf("create mac table db failed: %s, sql_ret %d.\n", sqlite3_errmsg(portal_args.portal_table), sql_ret);
        sqlite3_free(sql_errmsg);
        sqlite3_close(portal_args.portal_table);
        return FALSE;
    }

    snprintf(sql_buf, sizeof(sql_buf), "select * from nas_table");
    sql_ret = sqlite3_exec(portal_args.portal_table, sql_buf, portal_nas_table_read, 0, &sql_errmsg);
    if (sql_ret != SQLITE_OK) {
        printf("get nas table data failed: %s, sql_ret %d.\n", sqlite3_errmsg(portal_args.portal_table), sql_ret);
        sqlite3_free(sql_errmsg);
        sqlite3_close(portal_args.portal_table);
        return FALSE;
    }

    snprintf(sql_buf, sizeof(sql_buf), "select * from mac_table");
    sql_ret = sqlite3_exec(portal_args.portal_table, sql_buf, portal_mac_table_read, 0, &sql_errmsg);
    if (sql_ret != SQLITE_OK) {
        printf("get mac table data failed: %s, sql_ret %d.\n", sqlite3_errmsg(portal_args.portal_table), sql_ret);
        sqlite3_free(sql_errmsg);
        sqlite3_close(portal_args.portal_table);
        return FALSE;
    }

    sqlite3_close(portal_args.portal_table);

    return TRUE;
}

